package top.fangw.tenant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.seata.common.util.CollectionUtils;
import io.seata.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import top.fangw.common.core.exception.BizException;
import top.fangw.common.core.result.Result;
import top.fangw.tenant.pojo.dto.SysDictDataDto;
import top.fangw.tenant.pojo.entity.SysDictData;
import top.fangw.tenant.mapper.SysDictDataMapper;
import top.fangw.tenant.pojo.entity.SysDictType;
import top.fangw.tenant.service.ISysDictDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用途：字典的数据值 服务实现类
 * </p>
 *
 * @author fangw
 * @since 2024-02-23
 */
@Service
@RequiredArgsConstructor
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {

    @Override
    public void addSysDictData(SysDictDataDto sysDictDataDto) {
        if(StringUtils.isBlank(sysDictDataDto.getDictTypeId())){
            throw new BizException("字典类型字段不能为空");
        }
        //判断对应的value是否已经在该类型下面存在
        SysDictData exist = this.baseMapper.selectOne(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getValue, sysDictDataDto.getValue())
                .eq(SysDictData::getDictTypeId, sysDictDataDto.getDictTypeId()));
        if(exist != null){
            throw  new BizException("该字典类型下已经存在该value的数据");
        }
        SysDictData dictData = new SysDictData();
        BeanUtils.copyProperties(sysDictDataDto,dictData);
        dictData.setId(null);
        dictData.setCreatedBy("MYH");
        dictData.setCreatedByName("MYH");
        dictData.setCreatedAt(LocalDateTime.now());
        this.baseMapper.insert(dictData);
    }

    @Override
    public void deleteSysDictData(String id) {
        //判断字典状态是否为可删除
        SysDictData sysDictData = this.baseMapper.selectById(id);
        if(sysDictData != null){
            if("1".equals(sysDictData.getIsDeletable())){
                this.baseMapper.deleteSysDictData(id);
            }else{
                throw new BizException("字典数据状态为不可删除");
            }
        }
    }

    @Override
    public void updateDictData(SysDictDataDto sysDictDataDto) {
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(sysDictDataDto,sysDictData);
        this.baseMapper.updateById(sysDictData);
    }

    @Override
    public List<Map<String, Object>> queryDictDataByType(String type) {
        return this.baseMapper.queryDictDataByType(type);
    }

    @Override
    public List<SysDictData> getDictDateByDictId(String dictTypeId) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<SysDictData>()
                .eq(SysDictData::getDictTypeId,dictTypeId)
                .eq(SysDictData::getStatus,"1"));
    }

    @Override
    public Map<String, String> getDataByType(String type) {
        Map<String,String> resMap = new HashMap<>();
        List<SysDictDataDto> resList = this.baseMapper.getDataByType(type);
        if(CollectionUtils.isNotEmpty(resList)){
            resMap = resList.stream().collect(Collectors.toMap(SysDictDataDto::getValue,SysDictDataDto::getLabel,(x1, x2)->x1));
        }
        return resMap;
    }
}
